package com.letv.mytest.dao;

import java.util.List;

import jmind.pigg.annotation.DB;
import jmind.pigg.annotation.ReturnGeneratedId;
import jmind.pigg.annotation.SQL;

import com.letv.mytest.model.User;

/**
 * @author ash
 */
@DB(dataSource = "letv_boss", table = "user")
public interface UserDao {

    static final String SEARCH = " from #table where 1=1  " + " #if(:1!=null ) and name like :1 #end"
            + " #if(:2!=null ) and create_time > :2 #end" + " #if(:3!=null ) and create_time <=:3 #end";

    @ReturnGeneratedId
    @SQL("replace into #table(name,nickname,pwd,company,create_time,modify_time) values(:name,:nickname,:pwd,:company,now(),now())")
    public int add(User user);

    @SQL("SELECT *" + SEARCH)
    public List<User> search(String name, String start, String end);

    @SQL("SELECT COUNT(*) " + SEARCH)
    public int countSearch(String name, String start, String end);

    // 1,2,3
    @SQL("select * from #table #if(:1&&:name) where  name=:name #end limit 1")
    public User getUser(User u);

    @SQL("select *  from #table where 1=1 #if(:1) and name=:1 #end")
    public User getUserByName(String name);

    @SQL("delete from #table where uid in(:1)")
    public int delete(List<Integer> uids);

    @SQL("update #table set rids=:1.rids where uid=:1.uid")
    public int grant(User user);

    /**
     * 根据rid匹配用户，由于需要精确匹配用concat连接,rid前后都加上逗号
     * @param rid  %,132,%
     * @return
     */
    @SQL("select name from user where  concat(',',rids,',') like :1 ")
    public List<String> getNamesByRid(String rid);

}
